<?php
namespace app\admin\controller;

class Ordervip extends Admin {
	public function __construct() {
		parent::__construct();
		$this->model = 'OrderVip';
		$this->assign("model", $this->model);
	}
	public function index_stat() {

		return $this->fetch();
	}

	public function get_where_map() {
		$status = input('param.status/s');
		$startdate = input('param.startdate/s');
		$enddate = input('param.enddate/s');
		$dealerid = input('param.dealerid/d');
		$type = input('param.type/d');
        $paytype = input('param.paytype/d');
		$keywords = input('param.keywords/s');
		$whereDatas = [];
		if (!is_null($status) && $status != '3') {
			$whereDatas['status'] = $status;
		} else {
			if ($status == '3') {
				$whereDatas['status'] = ['in', ['0', '1']];
			} else {
				$whereDatas['status'] = '1';
			}
		}
		if ($dealerid) {
			$userids = model('AdminUser')->where('dealerid', $dealerid)->column('userid');
			$whereDatas['userid'] = ['in', $userids];
		} else {
			if ($this->uinfo['roleid'] == 4) {
				$dealerids = model('AdminUser')->where('puserid', $this->uinfo['userid'])->where('roleid', 6)->column('userid');
				$userids = model('AdminUser')->where('dealerid', 'in', $dealerids)->column('userid');
				$whereDatas['userid'] = ['in', $userids];
			} elseif ($this->uinfo['roleid'] == 6) {
				$userids = model('AdminUser')->where('dealerid', $this->uinfo['userid'])->column('userid');
				$whereDatas['userid'] = ['in', $userids];
			}
		}
		if ($type == 1) {
            $whereDatas['description'] = '购买荣耀会员';
        } else if ($type == 2) {
            $whereDatas['description'] = '续费荣耀会员';
        }
		if ($paytype == 1) { // 微信支付
            $whereDatas['paytype'] = 2;
        } else if ($paytype == 2) {
            $whereDatas['paytype'] = 6;
        } else if ($paytype == 3) {
            $whereDatas['paytype'] = 7;
        } else if ($paytype == 4) {
            $whereDatas['paytype'] = 8;
        }
		$starttime = $startdate ? strtotime($startdate) : 0;
		$endtime = $enddate ? strtotime($enddate) : 0;
		if ($starttime && $endtime) {
			$whereDatas['dateline'] = ['between', [$starttime, $endtime]];
		} elseif ($starttime && !$endtime) {
			$whereDatas['dateline'] = ['egt', $starttime];
		} elseif ($endtime && !$starttime) {
			$whereDatas['dateline'] = ['elt', $endtime];
		}
		if ($keywords) {
			$where['realname'] = ['like',  "%$keywords%"];
			$userid = model('AdminUser')->where($where)->column('userid');
			trace($userid);
			if ($userid) {
				$whereDatas['ordersn|userid'] = ['in', $userid];
			} else {
				$whereDatas['ordersn'] = ['like', "%$keywords%"];
			}
		}
		$whereDatas['ordertype'] = 0;
		return $whereDatas;
	}
	public function index() {
		$where['status'] = 0;
		if ($this->uinfo['roleid'] == 1) {
			$where['roleid'] = 6;
		} elseif ($this->uinfo['roleid'] == 4) {
			$where['roleid'] = 6;
			$where['puserid'] = $this->uinfo['userid'];
		} elseif ($this->uinfo['roleid'] == 6) {
			$where['userid'] = $this->uinfo['userid'];
		}
		$deallist = model('AdminUser')->where($where)->order('userid desc')->column('userid, username');

		$this->assign('deallist', $deallist);
		return $this->fetch();
	}
	public function audit() {
		$orderid = input('orderid/d');
		$status = input('status/d');
		$info = model($this->model)->where('id', $orderid)->find();
		if ($info['status'] > 0) {
			return json(['status' => 0, 'msg' => '不能重复操作！']);
		}
		if ($status == 1) {
			model("Order")->pay_success($info['ordersn'], $info['payprice'], 2);
		} else {
			model("Order")->pay_fail($info['ordersn'], $info['payprice'], '');
		}
		return json(['status' => 1, 'msg' => '操作成功！']);
	}
}